package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementResultDTO;
import com.neusoft.neuiotms.dcom.model.DutyArrangementResult;
import com.neusoft.neuiotms.dcom.repository.DutyArrangementRepository;
import com.neusoft.neuiotms.dcom.repository.DutyArrangementResultRepository;
import com.neusoft.neuiotms.dcom.repository.TeamGroupRepository;

@Component
public class DutyArrangementResultConverter extends AbstractConverter<DutyArrangementResult, DutyArrangementResultDTO> {

    @Autowired
    private DutyArrangementRepository dutyArrangementRepository;
    @Autowired
    private TeamGroupRepository teamGroupRepository;
    @Autowired
    private DutyArrangementResultRepository dutyArrangementResultRepository;

    @Override
    public DutyArrangementResult toModel(final DutyArrangementResultDTO dto) {

        DutyArrangementResult model;
        if (dto.isNew()) {
            model = new DutyArrangementResult();
        } else {
            model = this.dutyArrangementResultRepository.getOne(dto.getId());
        }

        model.setClassesCode(dto.getClassesCode());
        model.setClassesName(dto.getClassesName());
        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());

        if (null != dto.getDutyArrangementId()) {
            model.setDutyArrangement(this.dutyArrangementRepository.getOne(dto.getDutyArrangementId()));
        }

        if (null != dto.getTeamGroupId()) {
            model.setTeamGroup(this.teamGroupRepository.getOne(dto.getTeamGroupId()));
        }

        return model;
    }

    @Override
    public DutyArrangementResultDTO toDTO(final DutyArrangementResult model, final boolean forListView) {

        final DutyArrangementResultDTO dto = new DutyArrangementResultDTO();

        dto.setId(model.getId());
        dto.setClassesCode(model.getClassesCode());
        dto.setClassesName(model.getClassesName());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());

        if (null != model.getTeamGroup()) {
            dto.setTeamGroupId(model.getTeamGroup().getId());
            dto.setTeamGroupName(model.getTeamGroup().getName());
            dto.setColor(model.getTeamGroup().getColor());
        }

        if (null != model.getDutyArrangement()) {
            dto.setDutyArrangementId(model.getDutyArrangement().getId());
            dto.setDutyArrangementName(model.getDutyArrangement().getName());
        }

        return dto;
    }

}
